/*
 * Created on 14/04/2010
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package pagecode.pages.mantenimiento;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlCommandLink;
import javax.faces.component.html.HtmlForm;
import javax.faces.component.html.HtmlGraphicImage;
import javax.faces.component.html.HtmlInputTextarea;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGroup;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

import model.FiltroModel;
import model.consultas.CabeceraConsultaModel;

import org.ajax4jsf.ajax.html.AjaxPoll;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.myfaces.component.html.ext.HtmlDataTable;
import org.apache.myfaces.custom.crosstable.HtmlColumns;
import org.apache.myfaces.custom.div.Div;
import org.apache.myfaces.custom.navmenu.HtmlNavigationMenuItems;
import org.apache.myfaces.custom.navmenu.jscookmenu.HtmlCommandJSCookMenu;

import pagecode.PageCodeBase;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.constants.Border;
import ar.com.fdvs.dj.domain.constants.Font;
import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
import ar.com.fdvs.dj.domain.constants.Transparency;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;

/*import com.ibm.faces.component.html.HtmlCommandButton;
import com.ibm.faces.component.html.HtmlGraphicImage;
import com.ibm.faces.component.html.HtmlScriptCollector;*/
import com.stefanini.pe.bbva.mbl.in.utils.Constantes;

public class FiltroBean extends PageCodeBase {

	private static Logger logger = Logger.getLogger(FiltroBean.class);
	
	protected CabeceraConsultaModel cabeceraConsultaModel;
	protected FiltroModel filtroModel;
	protected HtmlPanelGroup dataTableGroup;
	protected HtmlColumns dinaProductoColumnDetalle;
	protected List columns;
	protected List rows;	   
	protected HtmlDataTable dinaProductosDetalle;
	protected HtmlOutputText MontoTotalTrans;
	protected HtmlCommandLink lnkRDirecto;
	//protected HtmlScriptCollector scriptCollector1;
	protected HtmlForm form1;
	protected HtmlGraphicImage image2;
	protected HtmlOutputText lblUsuario1;
	protected HtmlOutputText lblUsuario;
	protected HtmlOutputText lblFechaPrincipal;
	protected HtmlCommandLink lnkCerrarSession;
	protected HtmlOutputText lblCerrarSesion;
	//protected HtmlScriptCollector scriptCollector2;
	protected HtmlOutputText txtConsolidada;
	protected HtmlOutputText lblTipo;
	protected HtmlOutputText lblVer;
	protected HtmlCommandLink lnkRegresar;
	protected HtmlOutputText txthplRegresar;
	protected HtmlOutputText lblMontoTotalTransf;
	protected HtmlOutputText montoTotalTrans;
	protected HtmlOutputText txtError;
	protected HtmlCommandLink lnkInicio;
	protected HtmlOutputText txthplInicio;
	protected UIComponent poll;
	protected HtmlOutputText serverDate;
	protected HtmlOutputText estadoReproceso;
	protected HtmlPanelGroup grid;
	protected HtmlGraphicImage imageColor;
	protected HtmlDataTable lstSentencia;
	protected Div divi;
	protected HtmlOutputText cabeceraDinamica;
	protected HtmlOutputText valorDinamico;
	protected HtmlColumns columnasDinamicas;
	protected HtmlSelectOneMenu menuTipo;
	protected HtmlCommandButton btnEjecutar;
	protected HtmlInputTextarea txtParametroDet;
	protected AjaxPoll pollFecha;
	protected Div div0;
	protected HtmlOutputText lblFechaPrincipal_;
	protected HtmlCommandJSCookMenu Menu;
	protected HtmlNavigationMenuItems subMenus;
	protected HtmlOutputText lblParametroDet;
	protected HtmlPanelGroup getFechaActual;	
	
	public HtmlPanelGroup getDataTableGroup() {
		return dataTableGroup;
	}
	/**
	 * @param dataTableGroup The dataTableGroup to set.
	 */
	public void setDataTableGroup(HtmlPanelGroup dataTableGroup) {
		this.dataTableGroup = dataTableGroup;
	}
	
	
	public FiltroModel getFiltroModel() {
		if (filtroModel == null) {
			filtroModel = (FiltroModel) getFacesContext().getApplication()
					.createValueBinding("#{filtroModel}").getValue(
							getFacesContext());
		}
		return filtroModel;
	}
	public void setFiltroModel(FiltroModel filtroModel) {
		this.filtroModel = filtroModel;
	}
	
	/**
	 * @return Returns the cabeceraConsultaModel.
	 */
	public CabeceraConsultaModel getCabeceraConsultaModel() {
		if (cabeceraConsultaModel == null) {
			cabeceraConsultaModel = (CabeceraConsultaModel) getFacesContext()
					.getApplication().createValueBinding(
							"#{cabeceraConsultaModel}").getValue(
							getFacesContext());
		}
		return cabeceraConsultaModel;
	}
	/**
	 * @param cabeceraConsultaModel The cabeceraConsultaModel to set.
	 */
	public void setCabeceraConsultaModel(
			CabeceraConsultaModel cabeceraConsultaModel) {
		this.cabeceraConsultaModel = cabeceraConsultaModel;
	}
	
	public void onPageLoadConsultaBegin(FacesContext facescontext) {
		if (!this.isPostBack()) {
			try {
				this.getCabeceraConsultaModel().setBooIDCabGrupoRendered(true);
				this.getCabeceraConsultaModel().setBooIDCabClienteRendered(false);
			} catch (Exception e) {
				this.getFiltroModel().setLblError_Text(
						"Ocurrio un error al cargar la informacion");
				logger.error(e.getMessage(), e);
			}
		}
	}
	
	public void onPageLoadBegin(FacesContext facescontext) {
		// Type Java code to handle page load begin event here
		if (!this.isPostBack()) {			
			try {
				this.getFiltroModel().reset();	
			} catch (Exception e) {
				this.getFiltroModel().setLblError_Text(
						"Ocurrio un error al cargar la informacion");
				logger.error(e.getMessage(), e);
			}
		}
	}
	
	public String doBtnEjecutarAction(){
		String sentencia = null;
		String tipo = null;		
		List lista = null;
		
		try {
			logger.info("Inicio");
			
			this.getFiltroModel().reset();
			
			sentencia = this.getFiltroModel().getSentencia();
			tipo = this.getFiltroModel().getTipo();
			lista = this.getFiltroModel().getFiltroService().ejecutarConsultaFiltro(sentencia, tipo);
			
			if(lista != null && lista.size() == 2) {
				this.getFiltroModel().setSentenciaColumn((ArrayList)lista.get(0));
				this.getFiltroModel().setSentenciaDetalle((ArrayList)lista.get(1));
			}
			
			logger.info("Inicio");
		} catch(Exception e) {
			this.getFiltroModel().setLblError_Text(Constantes.MSJ_ERROR_INESPERADO);
			logger.error(e.getMessage(), e);
		}
			
		return "";
	}
	
	public String getLabel(HtmlSelectOneMenu selMenu,String val){
	 	String label=null;
	 	UISelectItems selItems=(UISelectItems)selMenu.getChildren().get(0);
	 	ArrayList selList=(ArrayList)selItems.getValue();
	 	Iterator iter=selList.iterator();
	 	while(iter.hasNext()){
		 	SelectItem sel=(SelectItem)iter.next();
		 	System.out.println("The select item is "+sel.getLabel());
		 	System.out.println("The select item value "+sel.getValue());
		 	if(sel.getValue().equals(val))label=sel.getLabel().toString();
		}
	 	return label;
 	}
	
	
	public Style retornaStyleHeader(){
		Style headerStyle = new Style();
		headerStyle.setBorderBottom(Border.PEN_1_POINT);
		headerStyle.setHorizontalAlign(HorizontalAlign.CENTER);
		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
		headerStyle.setBackgroundColor(Color.decode("#316495"));
		headerStyle.setTextColor(Color.WHITE);
		headerStyle.setTransparency(Transparency.OPAQUE);
		headerStyle.setFont(new Font(7, Font._FONT_VERDANA, true));
		headerStyle.setBorder(Border.THIN);
		return headerStyle;
	}
	
	public Style retornaStyleDetailRigth(){
		Style detailStyle = new Style();
		detailStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
		detailStyle.setBorderBottom(Border.PEN_1_POINT);
		detailStyle.setTransparency(Transparency.OPAQUE);
		detailStyle.setFont(new Font(7, Font._FONT_VERDANA, true));
		detailStyle.setBorder(Border.THIN);
		detailStyle.setPattern("#,##0.00");
		return detailStyle;
	}
	public Style retornaStyleDetailCenter(){
		Style detailStyle = new Style();
		detailStyle.setHorizontalAlign(HorizontalAlign.CENTER);
		detailStyle.setBorderBottom(Border.PEN_1_POINT);
		detailStyle.setTransparency(Transparency.OPAQUE);
		detailStyle.setFont(new Font(7, Font._FONT_VERDANA, true));
		detailStyle.setBorder(Border.THIN);
		detailStyle.setPattern("#,##0.00");
		return detailStyle;
	}
	public Style retornaStyleDetailRigthBold(){
		Style detailStyle = new Style();
		detailStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
		detailStyle.setBorderBottom(Border.PEN_1_POINT);
		detailStyle.setTransparency(Transparency.OPAQUE);
		detailStyle.setFont(new Font(7, Font._FONT_VERDANA, true));
		detailStyle.setFont(Font.VERDANA_MEDIUM_BOLD);
		detailStyle.setBorder(Border.THIN);
		return detailStyle;
	}
	public Style retornaStyleDetailLeft(){
		Style detailStyle = new Style();
		detailStyle.setHorizontalAlign(HorizontalAlign.LEFT);
		detailStyle.setBorderBottom(Border.PEN_1_POINT);
		detailStyle.setTransparency(Transparency.OPAQUE);
		detailStyle.setFont(new Font(7, Font._FONT_VERDANA, true));
		detailStyle.setBorder(Border.THIN);
		return detailStyle;
	}	
	
	private boolean isPostBack() {
		if (this.getFacesContext().getExternalContext()
				.getRequestParameterMap().get("isPostBackFiltro") == null) {
			return false;
		} else {
			return true;
		}
	}
		
	private boolean isValid(String strCodClienteTest) {
		boolean flag = true;
		if(StringUtils.isEmpty(strCodClienteTest)){
			flag = false;
		}else{
			if(StringUtils.isNumeric(strCodClienteTest)){
				flag = true;
			}else{
				flag = false;
			}
		}
		return flag;
	}
    
	/**
	 * @return Returns the dinaProductoColumnDetalle.
	 */
	public HtmlColumns getDinaProductoColumnDetalle() {
		return dinaProductoColumnDetalle;
	}
	/**
	 * @param dinaProductoColumnDetalle The dinaProductoColumnDetalle to set.
	 */
	public void setDinaProductoColumnDetalle(
			HtmlColumns dinaProductoColumnDetalle) {
		this.dinaProductoColumnDetalle = dinaProductoColumnDetalle;
	}
	/**
	 * @return Returns the dinaProductosDetalle.
	 */
	public HtmlDataTable getDinaProductosDetalle() {
		if (dinaProductosDetalle == null) {
			dinaProductosDetalle = (HtmlDataTable) findComponentInRoot("dinaProductosDetalle");
		}
		return dinaProductosDetalle;
	}
	/**
	 * @param dinaProductosDetalle The dinaProductosDetalle to set.
	 */
	public void setDinaProductosDetalle(HtmlDataTable dinaProductosDetalle) {
		this.dinaProductosDetalle = dinaProductosDetalle;
	}
	/*protected HtmlScriptCollector getScriptCollector1() {
		if (scriptCollector1 == null) {
			scriptCollector1 = (HtmlScriptCollector) findComponentInRoot("scriptCollector1");
		}
		return scriptCollector1;
	}*/
	protected HtmlForm getForm1() {
		if (form1 == null) {
			form1 = (HtmlForm) findComponentInRoot("form1");
		}
		return form1;
	}
	protected HtmlGraphicImage getImage2() {
		if (image2 == null) {
			image2 = (HtmlGraphicImage) findComponentInRoot("image2");
		}
		return image2;
	}
	protected HtmlOutputText getLblUsuario1() {
		if (lblUsuario1 == null) {
			lblUsuario1 = (HtmlOutputText) findComponentInRoot("lblUsuario1");
		}
		return lblUsuario1;
	}
	protected HtmlOutputText getLblUsuario() {
		if (lblUsuario == null) {
			lblUsuario = (HtmlOutputText) findComponentInRoot("lblUsuario");
		}
		return lblUsuario;
	}
	protected HtmlOutputText getLblFechaPrincipal() {
		if (lblFechaPrincipal == null) {
			lblFechaPrincipal = (HtmlOutputText) findComponentInRoot("lblFechaPrincipal");
		}
		return lblFechaPrincipal;
	}
	protected HtmlCommandLink getLnkCerrarSession() {
		if (lnkCerrarSession == null) {
			lnkCerrarSession = (HtmlCommandLink) findComponentInRoot("lnkCerrarSession");
		}
		return lnkCerrarSession;
	}
	protected HtmlOutputText getLblCerrarSesion() {
		if (lblCerrarSesion == null) {
			lblCerrarSesion = (HtmlOutputText) findComponentInRoot("lblCerrarSesion");
		}
		return lblCerrarSesion;
	}
	/*protected HtmlScriptCollector getScriptCollector2() {
		if (scriptCollector2 == null) {
			scriptCollector2 = (HtmlScriptCollector) findComponentInRoot("scriptCollector2");
		}
		return scriptCollector2;
	}*/
	protected HtmlOutputText getTxtConsolidada() {
		if (txtConsolidada == null) {
			txtConsolidada = (HtmlOutputText) findComponentInRoot("txtConsolidada");
		}
		return txtConsolidada;
	}
	protected HtmlOutputText getLblTipo() {
		if (lblTipo == null) {
			lblTipo = (HtmlOutputText) findComponentInRoot("lblTipo");
		}
		return lblTipo;
	}
	protected HtmlOutputText getLblVer() {
		if (lblVer == null) {
			lblVer = (HtmlOutputText) findComponentInRoot("lblVer");
		}
		return lblVer;
	}	
	protected HtmlCommandLink getLnkRegresar() {
		if (lnkRegresar == null) {
			lnkRegresar = (HtmlCommandLink) findComponentInRoot("lnkRegresar");
		}
		return lnkRegresar;
	}
	protected HtmlOutputText getTxthplRegresar() {
		if (txthplRegresar == null) {
			txthplRegresar = (HtmlOutputText) findComponentInRoot("txthplRegresar");
		}
		return txthplRegresar;
	}
	protected HtmlOutputText getLblMontoTotalTransf() {
		if (lblMontoTotalTransf == null) {
			lblMontoTotalTransf = (HtmlOutputText) findComponentInRoot("lblMontoTotalTransf");
		}
		return lblMontoTotalTransf;
	}
	protected HtmlOutputText getMontoTotalTrans() {
		if (montoTotalTrans == null) {
			montoTotalTrans = (HtmlOutputText) findComponentInRoot("montoTotalTrans");
		}
		return montoTotalTrans;
	}
	protected HtmlOutputText getTxtError() {
		if (txtError == null) {
			txtError = (HtmlOutputText) findComponentInRoot("txtError");
		}
		return txtError;
	}
	protected HtmlCommandLink getLnkInicio() {
		if (lnkInicio == null) {
			lnkInicio = (HtmlCommandLink) findComponentInRoot("lnkInicio");
		}
		return lnkInicio;
	}
	protected HtmlOutputText getTxthplInicio() {
		if (txthplInicio == null) {
			txthplInicio = (HtmlOutputText) findComponentInRoot("txthplInicio");
		}
		return txthplInicio;
	}

	protected UIComponent getPoll() {
		if (poll == null) {
			poll = (UIComponent) findComponentInRoot("poll");
		}
		return poll;
	}
	protected HtmlOutputText getServerDate() {
		if (serverDate == null) {
			serverDate = (HtmlOutputText) findComponentInRoot("serverDate");
		}
		return serverDate;
	}
	protected HtmlOutputText getEstadoReproceso() {
		if (estadoReproceso == null) {
			estadoReproceso = (HtmlOutputText) findComponentInRoot("estadoReproceso");
		}
		return estadoReproceso;
	}
	protected HtmlPanelGroup getGrid() {
		if (grid == null) {
			grid = (HtmlPanelGroup) findComponentInRoot("grid");
		}
		return grid;
	}
	protected HtmlGraphicImage getImageColor() {
		if (imageColor == null) {
			imageColor = (HtmlGraphicImage) findComponentInRoot("imageColor");
		}
		return imageColor;
	}
	protected HtmlDataTable getLstSentencia() {
		if (lstSentencia == null) {
			lstSentencia = (HtmlDataTable) findComponentInRoot("lstSentencia");
		}
		return lstSentencia;
	}
	protected Div getDivi() {
		if (divi == null) {
			divi = (Div) findComponentInRoot("divi");
		}
		return divi;
	}
	protected HtmlOutputText getCabeceraDinamica() {
		if (cabeceraDinamica == null) {
			cabeceraDinamica = (HtmlOutputText) findComponentInRoot("cabeceraDinamica");
		}
		return cabeceraDinamica;
	}
	protected HtmlOutputText getValorDinamico() {
		if (valorDinamico == null) {
			valorDinamico = (HtmlOutputText) findComponentInRoot("valorDinamico");
		}
		return valorDinamico;
	}
	protected HtmlColumns getColumnasDinamicas() {
		if (columnasDinamicas == null) {
			columnasDinamicas = (HtmlColumns) findComponentInRoot("columnasDinamicas");
		}
		return columnasDinamicas;
	}
	protected HtmlSelectOneMenu getMenuTipo() {
		if (menuTipo == null) {
			menuTipo = (HtmlSelectOneMenu) findComponentInRoot("menuTipo");
		}
		return menuTipo;
	}
	protected HtmlCommandButton getBtnEjecutar() {
		if (btnEjecutar == null) {
			btnEjecutar = (HtmlCommandButton) findComponentInRoot("btnEjecutar");
		}
		return btnEjecutar;
	}
	protected HtmlInputTextarea getTxtParametroDet() {
		if (txtParametroDet == null) {
			txtParametroDet = (HtmlInputTextarea) findComponentInRoot("txtParametroDet");
		}
		return txtParametroDet;
	}
	protected AjaxPoll getPollFecha() {
		if (pollFecha == null) {
			pollFecha = (AjaxPoll) findComponentInRoot("pollFecha");
		}
		return pollFecha;
	}
	protected Div getDiv0() {
		if (div0 == null) {
			div0 = (Div) findComponentInRoot("div0");
		}
		return div0;
	}
	protected HtmlOutputText getLblFechaPrincipal_() {
		if (lblFechaPrincipal_ == null) {
			lblFechaPrincipal_ = (HtmlOutputText) findComponentInRoot("lblFechaPrincipal_");
		}
		return lblFechaPrincipal_;
	}
	protected HtmlNavigationMenuItems getSubMenus() {
		if (subMenus == null) {
			subMenus = (HtmlNavigationMenuItems) findComponentInRoot("subMenus");
		}
		return subMenus;
	}
	protected HtmlOutputText getLblParametroDet() {
		if (lblParametroDet == null) {
			lblParametroDet = (HtmlOutputText) findComponentInRoot("lblParametroDet");
		}
		return lblParametroDet;
	}
	protected HtmlPanelGroup getGetFechaActual() {
		if (getFechaActual == null) {
			getFechaActual = (HtmlPanelGroup) findComponentInRoot("getFechaActual");
		}
		return getFechaActual;
	}
}